﻿myApp.controller("BuyRentController", function ($scope, $http, $modal, modalService, $sce, ngTableParams, $filter) {
    document.title = "Penang Realty BUY OR RENT WITH US 世界地產 BUY RENT";
    $scope.$emit('newPageLoaded', { 'title': 'Penang Realty BUY OR RENT WITH US 世界地產 BUY RENT', 'description': 'We have variety of properties for you to choose.' });
    function pageInit() {
        $scope.loader = { loading: false };

    }
    pageInit();
    $scope.property = {

    };

    $scope.save = function () {
        $scope.loader.loading = true;


        window.scrollBy(0, -10000);
        if (!validation())
            return;

        url = 'api/BuyRent/save';
        
        $http.post(url, $scope.property)
             .success(function (result) {
                 console.log("Data return!");
                 console.log(result);
                 $scope.loader.loading = false;
                 $scope.data.searchData = result;
                 $scope.info = "Your request has been submitted successfully. We thank you for your enquiry.";

             })
             .error(function (result) {
                 console.log("Error");
                 console.log(result);
                 $scope.loader.loading = false;
                 $scope.error = "Your request cannot be submitted now. Please contact our office directly for us to serve you immediately.";
             }
         )
    }


    $scope.clearEntry = function () {
        $scope.property.name = "";
        $scope.property.phone = "";
        $scope.property.phone = "";
        $scope.property.locationKeyword = "";
        $scope.property.propertyType = "Residential";
        $scope.property.submissionType = "Buy";
        $scope.property.useInvest = "";
        $scope.property.remark = "";
        $scope.property.price = 0;
        $scope.close();
    }

    $scope.close = function () {
        $scope.warning = null;
        $scope.info = null;
        $scope.error = null;
    }

    function validation() {
        $scope.warning = [];

        if (!$scope.property.locationKeyword)
            $scope.warning.push({ msg: "Location must not be empty!" });
        if (!$scope.property.price)
            $scope.warning.push({ msg: "Budget must not be empty!" });
        if (!$scope.property.name)
            $scope.warning.push({ msg: "Name must not be empty!" });
        if (!$scope.property.phone)
            $scope.warning.push({ msg: "Phone Number must not be empty!" });
        
        if ($scope.warning.length > 0)
            return false;
        else {
            $scope.warning = null;
            return true;
        }
    }
})